<template>
  <BasicModal width="700" v-bind="$attrs" cancelText="关闭" okText="提交"
    :title="getTitle"
    :canFullscreen="false"
    :centered="true"
    :destroyOnClose="true"
    :okButtonProps="{ disabled: isUploadingRef }"
    @register="registerModal"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" :model="modelRef" />
  </BasicModal>

</template>

<script lang="ts" setup name="bankBalance-list">
  import { ref, reactive, toRaw } from 'vue';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useMessage } from "/@/hooks/web/useMessage";
  import { saveOrUpdate, queryById } from "/@/views/full/bank/types/BankRunningReportSet.api";
  import { formSchema } from "/@/views/full/bank/types/BankRunningReportSet.data";
  import { useUserStore } from '/@/store/modules/user';

  const $message = useMessage();

  // 声明Emits
  const emit = defineEmits(['success', 'register']);

  // 标题
  const getTitle = ref<any>('系统对账 - 设置');

  //用户缓存
  const userStore = useUserStore();
  const relTenantValue = ref('');

  // 表单modal
  const modelRef = ref<any>({})
  const isUpdate = ref(true);
  const isUploadingRef = ref(false);
  //二、初始化数据 -----------------------------------------------------------
  const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
    setModalProps({confirmLoading: false});
    isUpdate.value = !!data?.isUpdate;
    await resetFields()

    modelRef.value = {};
    let record = data?.record || {};
    // 判断操作是新增还是修改
    getTitle.value = record?.id ? '编辑' : '新增';

    relTenantValue.value = String(userStore.getTenant)

    if (isUpdate.value) {
      // 流水数据
      record = await queryById({id:data?.record?.id});
      modelRef.value = record;
      data.record = record
      //console.info("data@@@@", data.record)

      // filter 不为空
      let itemIds = record.itemIds && record.itemIds.split(",")
      let bankIds = [], ddlxIds = [];
      if (record.type == '1') {
        bankIds = itemIds <= 0 ? [] : itemIds.filter(item => item)
      } else {
        ddlxIds = itemIds <= 0 ? [] : itemIds.filter(item => item)
      }

      modelRef.value = {
        bankTenantIds: bankIds,
        ddlxTenantIds: ddlxIds
      };

    } else {
      modelRef.value = {};
    }

    await setFieldsValue(record)
    //model.bankId = record.bankId
  });

  //初始化 -------------------------------------------------------------

  //表单配置
  const [registerForm, {resetFields, setFieldsValue, validate}] = useForm({
    schemas: formSchema,
    labelWidth: 120,
    baseColProps: {span: 20},
    showActionButtonGroup: false,
  });

  //表单提交事件
  async function handleSubmit() {
    try {
      setModalProps({confirmLoading: true});
      const values = await validate();
      await saveOrUpdate(values).then((res) => {
        if (res.success) {
          $message.createMessage.success(res.message);
          //关闭弹窗
          closeModal();
          emit("success")
        } else {
          $message.createMessage.warning(res.message);
        }
      });
    } catch (e) {
      console.log("handleSubmit - 提交校验出错", e);
    } finally {
      setModalProps({confirmLoading: false});
    }
  }

  // 设置itemIds 值
  function changeItemIds(record) {
    
  }
</script>
<style scoped></style>
